#include"Items.h"
void AddItem(LIST_ITEM &list,int kind,CStar *star,CMushroom *mushroom, CFlower *flower,CCoin *coin)
{
	NODE_ITEM *p=new NODE_ITEM;
	p->kind=kind;
    if(kind==33) p->star=star;
	else if(kind==55) p->flower=flower;
	else if(kind==31||p->kind==22) p->coin=coin;
	else p->mushroom=mushroom;
	p->next=NULL;
	if(list.pHead==NULL)
	{
		list.pHead=p;
		list.pTail=list.pHead;
	}
	else
	{
		list.pTail->next=p;
		list.pTail=p;
	}
}
void RemoveHead(LIST_ITEM &list)
{
	NODE_ITEM *p;
	if(list.pHead!=NULL)
	{
		p=list.pHead;
		if(list.pHead->next!=NULL)
		{
			list.pHead=list.pHead->next;
			delete p;
		}
		else
		{
			p=list.pHead;
			list.pHead=list.pTail=NULL;
			delete p;
		}
	//	if(list.pHead==NULL) list.pTail=NULL;
	}
}
void RemoveTail(LIST_ITEM &list)
{
	NODE_ITEM *p;
	p=list.pHead;
	while(p->next!=NULL&&p->next->next!=NULL) p=p->next;
	list.pTail=p;
	p->next=p->next->next;
	delete (p->next);
}
int RemoveNode(LIST_ITEM &list,NODE_ITEM *p)
{
	if(p!=list.pHead)
	{
		if(p->next!=NULL)
		{
			p->flower=p->next->flower;
			p->kind=p->next->kind;
			p->mushroom=p->next->mushroom;
			p->star=p->next->star;
			if(p->next->next==NULL)
			{
				NODE_ITEM *tam=p->next;
				p->next=NULL;
				list.pTail=p;
				delete tam;
			}
			else
			{
				NODE_ITEM *q=p->next->next;
				NODE_ITEM *tam=p->next;
				p->next=q;
				delete tam;
			}
			return 0;
		}
		else return 2;
	}
	else return 1;
}
CItems::CItems()
{
	
	star=NULL;
	mushroom=NULL;
	flower=NULL;
	coin=NULL;
	list.pHead=list.pTail=NULL;
	minicoin=new CSprite(false,0,177,25,25,0,2,0);
}
void CItems::GetItems(Object object)
{
	
	if(object.kind==44||object.kind==77)
	{
		mushroom=new CMushroom(object.kind,object.x,object.y,object.width,object.height);
			AddItem(list,object.kind,star,mushroom,flower,coin);
	}
	else
	 if(object.kind==33)
	{
		star=new CStar(object.x,object.y,object.width,object.height);
			AddItem(list,object.kind,star,mushroom,flower,coin);
	}
	 else
	 if(object.kind==55)
	 {
		 flower=new CFlower(object.x,object.y,object.width,object.height);
		 AddItem(list,object.kind,star,mushroom,flower,coin);
	 }
	 else
		
	{
		coin =new CCoin(object.kind,object.x,object.y,object.width,object.height);
		AddItem(list,object.kind,star,mushroom,flower,coin);
	}
	
}
void CItems::DeleteItems()
{
	NODE_ITEM *p;
	while(list.pHead!=NULL)
	{
		p=list.pHead;
		list.pHead=p->next;
		delete p;
	}
}
void CItems::Move(CStaticObjects *StaticObjects,RECT screen)
{
	NODE_ITEM *p;
	p=list.pHead;
	while(p!=NULL)
	{
		if(p->kind==33) p->star->Move(StaticObjects,screen);
		else if(p->kind==44||p->kind==77)p->mushroom->Move(StaticObjects,screen);
		p=p->next;
	}
}
int CItems::CollisionWithMario(int *kind,RECT marioRect)
{
	marioRect.left=marioRect.left+10;
	marioRect.right=marioRect.right-10;
	NODE_ITEM *p;
	p=list.pHead;
	int kq=0;
	while(p!=NULL)
	{
		if(p->kind==33) 
		{
			kq=p->star->CollisionWithMario(marioRect);
			*kind=33;
		}
		else if(p->kind==55) 
		{
			kq=p->flower->CollisionWithMario(marioRect);
			*kind=55;
		}
		else if(p->kind==31||p->kind==22)
			{
				kq=p->coin->CollisionWithMario(marioRect);
				*kind=31;
			}
			else
			{
				kq=p->mushroom->CollisionWithMario(marioRect);
				if(p->kind==44) *kind=44;
				else *kind=77;
			}
		if(kq!=0) return kq;
		p=p->next;
	}
}
void CItems::Render(LPD3DXSPRITE sprite, LPDIRECT3DTEXTURE9 image, int time,int xcamara)
{
	NODE_ITEM *p;
	int i=0;
	p=list.pHead;
	while(p!=NULL)
	{
		if(p->kind==33) 
		{
			p->star->Render(sprite,image,time,xcamara);
		}
		else if(p->kind==55) 
		{
			p->flower->Render(sprite,image,time,xcamara);
		}
		else if(p->kind==31||p->kind==22)
		{
			p->coin->Render(sprite,image,time,xcamara);
		}
		else 
		{
			p->mushroom->Render(sprite,image,time,xcamara);
		}
		p=p->next;
	}
	minicoin->Next();
	minicoin->Render(sprite,image,145,12);
}
CItems::~CItems()
{
	//if(Items!=NULL) delete Items;
	if(star!=NULL) delete star;
	if(coin!=NULL) delete coin;
	if(flower!=NULL) delete flower;
	if(mushroom!=NULL) delete mushroom;
}